package com.scsj.supermarket.view.fragment.followmodel;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.FollowGoodsAdapter;
import com.scsj.supermarket.interfaces.GoodsType;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品关注
 */
public class FollowGoods extends BaseFragment {

    private Boolean isEdit = false;// 商品关注 是否为编辑状态

    private RecyclerView recyclerview;
    private FollowGoodsAdapter followGoodsAdapter;
    private List<Map<String, String>> list;

    private TextView cancel_follow;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_follow_goods, null);

        initView(view, null);
        initListener();
        initData();

        return view;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        followGoodsAdapter = new FollowGoodsAdapter(getHoldingActivity());
        recyclerview.setAdapter(followGoodsAdapter);

        cancel_follow = view.findViewById(R.id.cancel_follow);
        cancel_follow.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {
        followGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (isEdit) {
                    Map map = (Map) adapter.getItem(position);
                    if (map.get("isSelectd").equals("0")) {
                        map.put("isSelectd", "1");
                    } else {
                        map.put("isSelectd", "0");
                    }
                    adapter.setData(position, map);
                } else {
                    SkipUtils.toGoodsDetails(getHoldingActivity(), GoodsType.common);
                }
            }
        });
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("type", "0");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "1");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "2");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("isSelectd", "0");
        list.add(map);
        map = new HashMap<>();
        map.put("type", "0");
        map.put("isSelectd", "0");
        list.add(map);

        followGoodsAdapter.setNewData(list);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }


    public void setEdit(Boolean b) {
        isEdit = b;
        if (isEdit) {
            cancel_follow.setVisibility(View.VISIBLE);
        } else {
            cancel_follow.setVisibility(View.GONE);
        }
        followGoodsAdapter.setEdit(isEdit);
    }

}
